﻿using System;

namespace DataAccess.Repository
{
	public class WJDZZWLatestInfoRepository : IDisposable, IWJDZZWLatestInfoRepository
	{
		private KPIDbContext _KPIDbContext = new KPIDbContext();

		#region IDisposable Members

		public void Dispose()
		{
			if(_KPIDbContext != null)
			{
				_KPIDbContext.Dispose();
			}
		}

		#endregion

		#region IWJDZZWLatestInfoRepository Members

		public void AddLatestInfo(string lguid, string title, string url, string userName, string groupName)
		{
			if(!string.IsNullOrWhiteSpace(lguid) && !string.IsNullOrWhiteSpace(title) && !string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(groupName))
			{
				try
				{
					_KPIDbContext.Database.Initialize(false);

					var cmd = _KPIDbContext.Database.Connection.CreateCommand();
					cmd.CommandText = "KPI_AddLatestInfo";
					cmd.CommandType = System.Data.CommandType.StoredProcedure;

					System.Data.Common.DbParameter p1 = cmd.CreateParameter();
					p1.DbType = System.Data.DbType.String;
					p1.Direction = System.Data.ParameterDirection.Input;
					p1.ParameterName = "@LGUID";
					p1.Value = lguid;
					System.Data.Common.DbParameter p2 = cmd.CreateParameter();
					p2.DbType = System.Data.DbType.String;
					p2.Direction = System.Data.ParameterDirection.Input;
					p2.ParameterName = "@TITLE";
					p2.Value = title;
					System.Data.Common.DbParameter p3 = cmd.CreateParameter();
					p3.DbType = System.Data.DbType.String;
					p3.Direction = System.Data.ParameterDirection.Input;
					p3.ParameterName = "@URL";
					p3.Value = url;
					System.Data.Common.DbParameter p4 = cmd.CreateParameter();
					p4.DbType = System.Data.DbType.String;
					p4.Direction = System.Data.ParameterDirection.Input;
					p4.ParameterName = "@USERNAME";
					p4.Value = userName;
					System.Data.Common.DbParameter p5 = cmd.CreateParameter();
					p5.DbType = System.Data.DbType.String;
					p5.Direction = System.Data.ParameterDirection.Input;
					p5.ParameterName = "@GROUPNAME";
					p5.Value = groupName;

					cmd.Parameters.Add(p1);
					cmd.Parameters.Add(p2);
					cmd.Parameters.Add(p3);
					cmd.Parameters.Add(p4);
					cmd.Parameters.Add(p5);

					if(_KPIDbContext.Database.Connection.State != System.Data.ConnectionState.Open)
					{
						_KPIDbContext.Database.Connection.Open();
					}

					cmd.ExecuteNonQuery();
				}
				catch
				{
					throw;
				}
			}
		}

		public void DeleteLatestInfo(string guid)
		{
			if(!string.IsNullOrWhiteSpace(guid))
			{
				try
				{
					_KPIDbContext.Database.Initialize(false);

					var cmd = _KPIDbContext.Database.Connection.CreateCommand();
					cmd.CommandText = "KPI_DeleteLatestInfo";
					cmd.CommandType = System.Data.CommandType.StoredProcedure;

					System.Data.Common.DbParameter p1 = cmd.CreateParameter();
					p1.DbType = System.Data.DbType.String;
					p1.Direction = System.Data.ParameterDirection.Input;
					p1.ParameterName = "@LGUID";
					p1.Value = guid;

					cmd.Parameters.Add(p1);

					if(_KPIDbContext.Database.Connection.State != System.Data.ConnectionState.Open)
					{
						_KPIDbContext.Database.Connection.Open();
					}

					cmd.ExecuteNonQuery();
				}
				catch
				{
					throw;
				}
			}
		}

		public void KPIProgressReportWarning()
		{
			try
			{
				_KPIDbContext.Database.Initialize(false);

				var cmd = _KPIDbContext.Database.Connection.CreateCommand();
				cmd.CommandText = "KPI_KPIProgressWarning";
				cmd.CommandType = System.Data.CommandType.StoredProcedure;

				if(_KPIDbContext.Database.Connection.State != System.Data.ConnectionState.Open)
				{
					_KPIDbContext.Database.Connection.Open();
				}

				cmd.ExecuteNonQuery();
			}
			catch
			{
				throw;
			}
		}

		#endregion
	}
}
